package com.cpi.cornpp.futurepurchase.action;

import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;
import org.apache.struts.util.MessageResources;

import com.cpi.cornpp.futurepurchase.form.FuturePurchaseForm;
import com.cpi.cornpp.product.services.ProductService;
import com.cpi.cornpp.product.view.ProductView;

/**
 * Product List Action
 * @author Edgar Joao
 *
 */
public class ProductEditListAction extends MappingDispatchAction{
	/** Identifier default workflow*/
	private final static String DEFAULT = "default";//$NON-NLS-1$
	/** Identifier fail workflow*/
	private static final String FAILURE = "fail";
	/** List full key */
	private static final String LIST_CONTAINS_EQUAL_VALUE_LABEL = "common.element.exist";//$NON-NLS-1$	
	/** Error message name*/
	private final static String ERROR_MESSAGE = "ERROR_MESSAGE";//$NON-NLS-1$
	private static final String MESSAGE_SELECT_A_PRODUCT = "error.message.futurepurchase.select.product";//$NON-NLS-1$
	
	/**
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward goProductEditSelection(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		((FuturePurchaseForm)form).getProductViewSearch().setDescription(StringUtils.EMPTY);
		return mapping.findForward(DEFAULT);
	}
	
	/**
	 * Busqueda de productos
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward searchEditProducts(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		FuturePurchaseForm futurePurchaseForm = null;
		ProductView productView = null;		
		ProductService service = null;
		List products = null;
		
		futurePurchaseForm =  (FuturePurchaseForm) form;
		productView = futurePurchaseForm.getProductViewSearch();
		service = new ProductService();		
		products = service.getProductList(productView);		
		futurePurchaseForm.setResultProducts(products);
		request.setAttribute("listOfProducts", futurePurchaseForm.getResultProducts());
		
		form.reset(mapping, request);
		return mapping.findForward(DEFAULT);
	}		
	/**
	 * 
	 * Metodo para seleccionar un 
	 * producto de la lista
	 * @author Edgar Joao
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward selectEditProduct(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		MessageResources messageResources = null;
		String messageError = null;
		FuturePurchaseForm futurePurchaseForm = null;		
		ProductView productView = null;
		List listInSession = null;
		String forward = null;
		HttpSession httpSession = request.getSession(false);		
		futurePurchaseForm =  (FuturePurchaseForm) form;
		futurePurchaseForm.getProductViewSearch().getProductId();
		httpSession = request.getSession(false);
		messageResources = getResources(request);
		
		//Se verifica si el elmento seleccionado existe en la lista de productos
		listInSession = (List) httpSession.getAttribute("LIST_CUSTOMER_PURCHASE_PRODUCTS");	
		if(!StringUtils.equals(futurePurchaseForm.getProductViewSearch().getProductId().trim(), StringUtils.EMPTY)){
			if(listInSession != null){
				if(!listInSession.isEmpty()){
					Iterator iterator = listInSession.iterator();
					while (iterator.hasNext()) {
						ProductView view = (ProductView) iterator.next();
						if(StringUtils.equals(view.getProductId().trim(), futurePurchaseForm.getProductViewSearch().getProductId())){												
							messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), LIST_CONTAINS_EQUAL_VALUE_LABEL);						
							httpSession.setAttribute(ERROR_MESSAGE, messageError);
							ProductView pv = new ProductView();
							pv.setDescription(StringUtils.EMPTY);
							pv.setProductId(StringUtils.EMPTY);
							pv.setTon(StringUtils.EMPTY);
							futurePurchaseForm.setProductViewSearch(pv);											
							forward = FAILURE;
							break;
						}
					}			
				}
			}
		}else{
			messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), MESSAGE_SELECT_A_PRODUCT);
			httpSession.setAttribute(ERROR_MESSAGE, messageError);
			forward = FAILURE;
		}
		
		if(!StringUtils.equals(forward, FAILURE)){
			if (!CollectionUtils.isEmpty(futurePurchaseForm.getResultProducts())){
				Iterator iterator = futurePurchaseForm.getResultProducts().iterator();
				while (iterator.hasNext()) {
					ProductView view = (ProductView) iterator.next();
					if(org.apache.commons.lang.StringUtils.equals(view.getProductId().trim(), futurePurchaseForm.getProductViewSearch().getProductId())){
						productView = new ProductView();
						productView.setProductId(view.getProductId());
						productView.setDescription(view.getDescription());
						futurePurchaseForm.setProductViewSearch(productView);						
						forward = DEFAULT;
						break;
					}
				}
			}
		}
		form.reset(mapping, request);	
		return mapping.findForward(forward);
	}
	
}
